package com.example.lenovo.calculate;
import java.util.Stack;

/**
 * Created by lenovo on 2017/6/24.
 */

public class InfixToSuffix {
    //将中缀表达式转为后缀表达式
    Stack<String> myStack = new Stack<String>();
    String[] strs = new String[99];
    char[] symbol = {'+','-','*','/'};
    int count = 0; //记录当前数组内的元素个数
    boolean minus = false; //检验是否为负数
    public String Storage(String expression){
        //将字符串的内容转到栈中
        try{
            char[] chars = expression.toCharArray();
            String number = "";
            int i=0;
            if(chars[0] == '-'){
                number += "-";
                i = 1;
            }
            for(;i<chars.length;i++){
                if(chars[i] == '+'|| chars[i] == '-' || chars[i] == '*' || chars[i] == '/' || chars[i] == '(' || chars[i] == ')'){
                    //myStack.push(number); //将之前的数字存入栈
                    if(!number.equals("")){
                        strs[count] = number;
                        count++;
                    }
                    if(myStack.empty()){
                        myStack.push(String.valueOf(chars[i])); //栈内无元素直接入栈
                    }
                    else {
                        switch (chars[i]){
                            case '+':
                                while(myStack.peek().equals("*")||myStack.peek().equals("/")){
                                    strs[count] = myStack.pop();
                                    count++;
                                }
                                myStack.push(String.valueOf(chars[i]));
                                break;
                            case '-':
                                if(chars[i-1] == '('){
                                    minus = true;
                                    break;
                                }
                                while(myStack.peek().equals("*")||myStack.peek().equals("/")){
                                    strs[count] = myStack.pop();
                                    count++;
                                }
                                myStack.push(String.valueOf(chars[i]));
                                break;
                            case '*':
                                myStack.push(String.valueOf(chars[i]));
                                break;
                            case '/':
                                myStack.push(String.valueOf(chars[i]));
                                break;
                            case '(':
                                myStack.push(String.valueOf(chars[i]));
                                break;
                            case ')':
                                while(!myStack.peek().equals("(")){
                                    strs[count] = myStack.pop();
                                    count++;
                                }
                                myStack.pop();
                                break;
                        }
                    }
                    number = "";
                    if(minus) {
                        number += chars[i];
                        minus = false;
                    }
                }
                else {
                    //代表为数字或小数点
                    number += chars[i];
                }
                //遍历到最后一位
                if(i==chars.length-1){
                    if(chars[i] != ')'){
                        //验证最后一位是否为数字
                        strs[count] = number;
                        count++;
                    }
                }
            }
            while(!myStack.empty()){
                //将所有元素输出
                strs[count] = myStack.pop();
                count++;
            }
            Stack<String> doCal = new Stack<String>();
            for(int j=0;j<count;j++){
                if(!strs[j].equals("+")&&!strs[j].equals("/")&&!strs[j].equals("*")&&!strs[j].equals("-")){
                    //从左向右扫描，遇到数字压入堆栈
                    doCal.push(strs[j]);
                }
                else{
                    //将栈顶两个元素弹出并计算
                    char [] ca = strs[j].toCharArray();
                    switch (ca[0]){
                        case '+':
                            doCal.push(Double.toString(Double.valueOf(doCal.pop())+Double.valueOf(doCal.pop())));
                            break;
                        case '-':
                            doCal.push(Double.toString(-Double.valueOf(doCal.pop())+Double.valueOf(doCal.pop())));
                            break;
                        case '*':
                            doCal.push(Double.toString(Double.valueOf(doCal.pop())*Double.valueOf(doCal.pop())));
                            break;
                        case '/':
                            doCal.push(Double.toString(Double.valueOf(doCal.pop())/Double.valueOf(doCal.pop())));
                            break;
                    }
                }
            }
            return doCal.peek();
        }
        catch (Exception e){
            return "输入错误";
        }
    }
}
